﻿@inject IJSRuntime JSRuntime

<!-- 模态框（Modal） -->
<div class="modal fade" id="idModal" tabindex="-1" role="dialog" aria-labelledby="idModalTitle" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title text-left" id="idModalTitle"></h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body" id="idModalBody"></div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal" id="idConfirm" @onclick="ConfirmClicked">确定</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal" id="idYes" @onclick="YesClicked">是</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal" id="idNo" @onclick="NoClicked">否</button>
                <button type="button" class="btn btn-default" data-dismiss="modal" id="idCancel" @onclick="CancelClicked">取消</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>



@code { 

    TaskCompletionSource<MessageBoxReturn> TaskCompletionSource { get; set; }

    public async Task<MessageBoxReturn> ShowMessageBox(string messageHtml, string titleHtml,string confrimHtml="",string yesHtml="",string noHtml="",string cancelHtml="")
    {
        string[] paras = new string[]
        {
            messageHtml,titleHtml,confrimHtml,yesHtml,noHtml,cancelHtml
        };

        TaskCompletionSource = new TaskCompletionSource<MessageBoxReturn>();
        await JSRuntime.InvokeVoidAsync("ShowMessageBox", paras);
        return await TaskCompletionSource.Task;
    }

    public enum MessageBoxReturn
    {
        Confirm=0,
        Yes=1,
        No=2,
        Cancel=3
    }

    void ConfirmClicked()
    {
        if (TaskCompletionSource != null)
        {
            TaskCompletionSource.TrySetResult(MessageBoxReturn.Confirm);
        }
    }
    void YesClicked()
    {
        if (TaskCompletionSource != null)
        {
            TaskCompletionSource.TrySetResult(MessageBoxReturn.Yes);
        }
    }
    void NoClicked()
    {
        if (TaskCompletionSource != null)
        {
            TaskCompletionSource.TrySetResult(MessageBoxReturn.No);
        }
    }
    void CancelClicked()
    {
        if (TaskCompletionSource != null)
        {
            TaskCompletionSource.TrySetResult(MessageBoxReturn.Cancel);
        }
    }

    public void Toast(string title,string subtitle="",string content="",ToastType type=ToastType.info, int delay=5000,int pause_on_hover=1)
    {
        object[] paras = new object[]
        {
            title, subtitle, content, type, delay,pause_on_hover
        };
        JSRuntime.InvokeVoidAsync("Toast", paras);
    }

    public enum ToastType
    {
        info=0,
        warning=1,
        success=2,
        error=3
    }
}
